package indi.h.devmg.acquisition.snmp.mib.rules;

import java.util.ArrayList;
import java.util.List;

import indi.h.devmg.acquisition.snmp.mib.beans.CustomType;
import indi.h.devmg.acquisition.snmp.mib.beans.MIB;

public class CustomTypeRule implements Rule<CustomType> {

	private CustomType customType;

	private List<String> params;

	private String param;

	private MIB mib;

	public CustomTypeRule(MIB mib) {
		this.mib = mib;
		this.params = new ArrayList<>();
		this.params.add("STATUS");
		this.params.add("DESCRIPTION");
		this.params.add("SYNTAX");
		this.params.add("DISPLAY-HINT");
	}

	@Override
	public void init(String s) {
		// TODO Auto-generated method stub
		this.customType = new CustomType();
		String[] split = s.split("::=");
		this.customType.setName(split[0]);
		this.mib.getCustomTypes().put(customType.getName(), customType);
		this.customType.setType(split[1].split("--")[0].trim());
	}

	@Override
	public CustomType getTarget() {
		// TODO Auto-generated method stub
		return this.customType;
	}

	@Override
	public int addData(String s) {
		// TODO Auto-generated method stub
		addParams(s.trim());
		return 0;
	}

	protected boolean addParams(String s) {
		int indexOf = s.indexOf(" ");
		if (indexOf > 0 && params.contains(s.substring(0, indexOf))) {
			String substring = s.substring(0, indexOf);
			param = substring;
			addParam(s.substring(indexOf + 1));
			return false;
		} else {
			if (params.contains(s)) {
				param = s;
				return false;
			} else {
				addParam(s);
				return false;
			}
		}
	}

	protected void addParam(String s) {
		if (param == null)
			return;
		if (param.equals("SYNTAX")) {
			customType.setSyntax(customType.getSyntax() == null ? s : customType.getSyntax() + " " + s);
		} else if (param.equals("STATUS")) {
			customType.setStatus(s);
		} else if (param.equals("DESCRIPTION")) {
			customType.setDescription(customType.getDescription() == null ? s : customType.getDescription() + " " + s);
		} else if (param.equals("DISPLAY-HINT")) {
			customType
					.setDisplay_hint(customType.getDisplay_hint() == null ? s : customType.getDisplay_hint() + " " + s);
		}
	}

}
